package com.zzptc.lzh.myapp26.Fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.zzptc.lzh.myapp26.Adapter.UserAdapter;
import com.zzptc.lzh.myapp26.Bean.User;
import com.zzptc.lzh.myapp26.MainActivity;
import com.zzptc.lzh.myapp26.R;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;


public class FirstFragment extends Fragment {
    ListView lv;
    private UserAdapter adapter;
    private int permission=0;
    List<User>userList=new ArrayList<>();

    public FirstFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_first, container, false);//新变量接着后面那一串
        initView(view);
        setListener();
        return view;
    }

    private void setListener() {
        /*lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showToast("你点击了"+userList.get(position).getName());
            }
        });*/
        SharedPreferences sp=getActivity().getSharedPreferences("userinfo",Context.MODE_PRIVATE);
        permission=sp.getInt("permission",-1);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch(permission){
                    case 1:
                        showToast("您点击啦"+userList.get(position).getName());
                        break;
                    case 2:
                        if (userList.get(position).getPermission()==permission){
                            showToast("不可对一级用户进行操作");
                        }else{
                            showDialog(userList.get(position));
                        }
                        break;
                }
            }
        });
    }
    private void showDialog(final User user){
        AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
        builder.setIcon(R.mipmap.gg);
        builder.setTitle("删除用户");
        builder.setMessage("是否删除用户"+user.getName());
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                LitePal.delete(User.class,user.getId());
                userList.remove(user);//删除数据库里面的用户
                adapter.notifyDataSetChanged();
                showToast("已删除该用户");
            }
        });
        builder.setNegativeButton("否",null);
        builder.create().show();
    }

    private void initView(View view) {
        userList= LitePal.findAll(User.class);
        lv=view.findViewById(R.id.lv);
       adapter=new UserAdapter(getContext(),R.layout.user_list,userList);
        lv.setAdapter(adapter);
    }
    private void showToast(String str){
        Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
    }

}
